package edu.nf.takeoutdemo.web.admin;

import edu.nf.takeoutdemo.commons.base.BaseController;
import edu.nf.takeoutdemo.commons.result.PageResult;
import edu.nf.takeoutdemo.commons.result.Result;
import edu.nf.takeoutdemo.model.dto.DishDTO;
import edu.nf.takeoutdemo.model.dto.DishPageQueryDTO;
import edu.nf.takeoutdemo.model.entity.Dish;
import edu.nf.takeoutdemo.model.vo.DishVO;
import edu.nf.takeoutdemo.service.DishService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author zjl
 * @date 2025/10/25
 */
@RestController("adminDishController")
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/admin/dish")
public class DishController extends BaseController {
    private final DishService dishService;

    @PostMapping
    public Result<?> save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);
        return success();
    }

    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询：{}",dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return success(pageResult);
    }

    @DeleteMapping
    public Result<?> delete(@RequestParam List<Long> ids){
        log.info("菜品批量删除：{}",ids);
        dishService.deleteBatch(ids);
        return success();
    }

    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable("id") Long id){
        log.info("根据id查询菜品：{}",id);
        DishVO dishVO =  dishService.getByIdWithFlavor(id);
        return success(dishVO);
    }

    @PutMapping
    public Result<?> update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品：{}",dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return success();
    }

    @GetMapping("/list")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> dishList =  dishService.list(categoryId);
        return success(dishList);
    }

    @PostMapping("/status/{status}")
    public Result<?> startOrStop(@PathVariable("status") Integer status,Long id){
        log.info("status:{},id:{}",status,id);
        dishService.startOrStop(status,id);
        return success();
    }
}
